package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.UserMapper;
import cn.tedu._03vehicle.pojo.dto.UserLoginParam;
import cn.tedu._03vehicle.pojo.dto.UserRegParam;
import cn.tedu._03vehicle.pojo.entity.User;
import cn.tedu._03vehicle.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Api(tags = "01.用户管理模块")
@RestController
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;
    @PostMapping("reg")
    @ApiOperation("注册用户功能")
    @ApiOperationSupport(order = 10)
    public JsonResult reg(@Validated @RequestBody UserRegParam userRegParam) {
        log.debug("UserRegParam = "+ userRegParam);
        //先验证注册信息的合法性
        //1、username不能为空,先在接收的参数上加@Validated注解，在属性上加@NotNull注解就可以啦
        //先判断用户是否被占用
        int count= userMapper.selectByUsername(userRegParam.getUsername());
        if (count>0){//用户名被占用
            return new JsonResult(StatusCode.USERNAME_EXIST);
        }
        User user = new User();
        //复制属性
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user = "+ user);//打印看看，数据有没有问题
        int rows =userMapper.insertUser(user);
        if (rows == 0) {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }else {
            return JsonResult.ok();
        }
    }

    @PostMapping("login")
    @ApiOperation("登录功能")
    @ApiOperationSupport(order = 20)
    public JsonResult login(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("UserLoginParam = "+ userLoginParam);
        //先判断用户名和密码是否错误
       // UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam.getUsername(), userLoginParam.getPassword());
        UserVO userVO = userMapper.selectByUsernameAndPassword(userLoginParam);
        if (userVO == null) {
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }else {
            //将用户信息保存到session中
            session.setAttribute("user777888XXX",userVO);//这种方式，就把key写死了，实际不能这么用
            return JsonResult.ok(userVO);
        }
    }
    /*
    获取当前用户功能
     */
    @GetMapping("currentUser")
    @ApiOperation("获取当前用户功能")
    @ApiOperationSupport(order = 30)
    public JsonResult currentUser(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user777888XXX");//有可能获取的到，有可能没有，所以强转一下
//        if (userVO == null) {
//            return new JsonResult(StatusCode.NOT_LOGIN);
//        }else {
//            return JsonResult.ok(userVO);
//        }
        return JsonResult.ok(userVO);//后端不判断了，交给前端去判断去
    }
    /*
    退出功能
     */
    @GetMapping("logout")
    @ApiOperation("退出登录功能")
    @ApiOperationSupport(order = 40)
    public void logout(@ApiIgnore HttpSession session){
        session.removeAttribute("user777888XXX");
    }
}
